package cn.com.utt.common.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UttTool {

	/**
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @return 时间间隔
	 */
	public static String getTimeDuration(Timestamp startTime, Timestamp endTime) {
		long start, end;
		if (startTime == null) {
			start = System.currentTimeMillis();
		} else {
			start = startTime.getTime();
		}

		if (endTime == null) {
			end = System.currentTimeMillis();
		} else {
			end = endTime.getTime();
		}
		long time = end - start;
		String str = "";
		int day = (int) (time / (24 * 3600 * 1000));
		if (day > 0) {
			str += day + "天";
		}
		int leave1 = (int) (time % (24 * 3600 * 1000)); // 计算天数后剩余的毫秒数
		int hours = leave1 / (3600 * 1000);
		// 计算相差分钟数
		int leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
		int minutes = leave2 / (60 * 1000);
		// 计算相差秒数
		int leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
		int seconds = leave3 / 1000;
		if (hours > 0) {
			str += hours + "小时";
		}
		str += minutes + "分钟";
		str += seconds + "秒";
		return str;
	}

	/**
	 * @param str
	 * @return 判断字符串是否是数字
	 */
	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			int chr = str.charAt(i);
			if (chr < 48 || chr > 57) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @param email
	 * @return 判断是否是邮箱
	 */
	public static boolean isEmailFormat(String email) {
		if (email != null) {
			final String patternStr = "^([a-z0-9A-Z_]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			final Pattern pattern = Pattern.compile(patternStr);
			final Matcher mat = pattern.matcher(email);
			return mat.find();
		}
		return false;
	}

	/**
	 * @param phone
	 * @return 判断是否是手机号
	 */
	public static boolean isPhoneFormat(String phone) {
		if (phone != null) {
			final String patternStr = "^0?((13[0-9]|15[0-9]|18[0-9]|17[0-9]|14[57])[0-9]{8})$";
			final Pattern pattern = Pattern.compile(patternStr);
			final Matcher mat = pattern.matcher(phone);
			return mat.find();
		}
		return false;
	}

	/**
	 * @param ip
	 * @return 验证ip是否正确
	 */
	public static boolean isIP(String ip) {
		String pattern = "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";
		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(ip);
		return m.matches();
	}

	public static final String UNSAFE_STR = "'~`·!@#$%^&*()-+.\\\" ";
	public static final String SSID_UNSAFE_STR = "<>,%'\"&|;{}";
	
	public static boolean isSsidSafe(String str) {
		char ch;
		int temp;
		boolean safe = true; // 当包含非法字符时，返回false
		for (int i = 0; i <= (SSID_UNSAFE_STR.length() - 1); i++) {
			ch = SSID_UNSAFE_STR.charAt(i);
			temp = str.indexOf(ch);
			if (temp != -1) {
				safe = false;
				break;
			}
		}
		return safe;
	}
	

	public static boolean isSafe(String str) {
		char ch;
		int temp;
		boolean safe = true; // 当包含非法字符时，返回false
		for (int i = 0; i <= (UNSAFE_STR.length() - 1); i++) {
			ch = UNSAFE_STR.charAt(i);
			temp = str.indexOf(ch);
			if (temp != -1) {
				safe = false;
				break;
			}
		}
		return safe;
	}

	/** 年月日时分秒(无下划线) yyyyMMddHHmmss */
	public static final String dtLong = "yyyyMMddHHmmss";

	/**
	 * 返回系统当前时间(精确到毫秒),作为一个唯一的订单编号
	 * 
	 * @return 以yyyyMMddHHmmss为格式的当前系统时间
	 */
	public static String getOrderNum() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(dtLong);
		return df.format(date);
	}

	/**
	 * 产生随机的三位数
	 * 
	 * @return
	 */
	public static String getThree() {
		Random rad = new Random();
		return rad.nextInt(1000) + "";
	}

	public static String getOrderNo() {

		return getOrderNum() + getThree();
	}
	
	public static String getPrintSize(float size) { 
		DecimalFormat df = new DecimalFormat("0.00");
	    //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义  
	    if (size < 1024) {  
	        return String.valueOf(size) + "B";  
	    } else {  
	        //size = size / 1024; 
	    		size = Float.parseFloat(df.format(size/1024));
	    }  
	    //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位  
	    //因为还没有到达要使用另一个单位的时候  
	    //接下去以此类推  
	    if (size < 1024) {  
	        return String.valueOf(size) + "KB";  
	    } else {  
	    		size = Float.parseFloat(df.format(size/1024));
	    }  
	    if (size < 1024) {  
	        //因为如果以MB为单位的话，要保留最后1位小数，  
	        //因此，把此数乘以100之后再取余  
	        return String.valueOf(size) + "MB";  
	    } else {  
	        //否则如果要以GB为单位的，先除于1024再作同样的处理  
	    		size = Float.parseFloat(df.format(size/1024));  
	    		return String.valueOf(size) + "GB";  
	    }  
	} 
	
	public static int getFileLength(String downloadUrl) throws IOException{  
        if(downloadUrl == null || "".equals(downloadUrl)){  
            return 0;   
        }  
        URL url = new URL(downloadUrl);  
        HttpURLConnection conn = null;  
        try {  
            conn = (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("HEAD");  
            return conn.getContentLength();  
        } catch (IOException e) {  
            return 0;  
        } finally {  
            conn.disconnect();  
      }  
   }

	public static void main(String[] args) {
		System.out.println(isPhoneFormat(null));
	}
}
